BUILD:=./build
YOS_IMG_NAME:=${BUILD}/yos.img
YOS_VMDK_NAME:=${BUILD}/yos.vmdk

all: clean build

build: ${BUILD}/boot/boot.o ${BUILD}/boot/loader.o ${BUILD}/kernel/main.bin
	ndisasm -b 16 -o 0xD00 ${BUILD}/boot/loader.o > ${BUILD}/boot/loader16.disasm
	ndisasm -b 32 -o 0xDA2 -e 0xA2 ${BUILD}/boot/loader.o > ${BUILD}/boot/loader32.disasm

	nm ${BUILD}/kernel/main.bin | sort > ${BUILD}/kernel/nm.txt
	objdump -D -M intel ${BUILD}/kernel/main.bin > ${BUILD}/kernel/objdump.txt
	readelf -e ${BUILD}/kernel/main.bin > ${BUILD}/kernel/readelf.txt

	objcopy -O binary ${BUILD}/kernel/main.bin ${BUILD}/kernel/main2.bin

	bximage -q -hd=16 -func=create -sectsize=512 -imgmode=flat ${YOS_IMG_NAME}
	dd if=${BUILD}/boot/boot.o of=${YOS_IMG_NAME} bs=512 seek=0 count=1 conv=notrunc
	dd if=${BUILD}/boot/loader.o of=${YOS_IMG_NAME} bs=512 seek=1 count=2 conv=notrunc
	dd if=${BUILD}/kernel/main.bin of=${YOS_IMG_NAME} bs=512 seek=5 count=60 conv=notrunc

${BUILD}/boot/%.o: boot/%.asm
	nasm -I boot/include/ -o $@ $<

${BUILD}/kernel/main.bin: ${BUILD}/kernel/main.o ${BUILD}/kernel/print/print.o ${BUILD}/kernel/interrupt/init.o
	ld -m elf_i386 -Ttext 0xc0001500 -e main -o $@ $^

${BUILD}/kernel/main.o: kernel/main.c
	gcc -m32 -c -g -o $@ $<

${BUILD}/kernel/interrupt/init.o: kernel/interrupt/init.c
	gcc -m32 -c -g -o $@ $<

${BUILD}/kernel/print/print.o: kernel/print/print.asm
	nasm -f elf32 -g -o $@ $<

clean:
	rm -rf ${BUILD}
	mkdir -p ${BUILD}/boot ${BUILD}/kernel/print ${BUILD}/kernel/interrupt

bochs: all
	bochs -q -f bochsrc

qemug: all
	qemu-system-x86_64 -m 32M -hda ${YOS_IMG_NAME} -s -S

qemu: all
	qemu-system-x86_64 -m 32M -boot c -hda ${YOS_IMG_NAME}

vmdk: all ${YOS_VMDK_NAME}

${YOS_VMDK_NAME}: ${YOS_IMG_NAME}
	qemu-img convert -O vmdk $< $@

# $@: 目标文件集， $^: 依赖文件集， $<: 依赖文件集的第一个
